package com.example.demo.mybatis.controller;

import com.example.demo.mybatis.model.Coffee;
import com.example.demo.mybatis.model.CoffeeOrder;
import com.example.demo.mybatis.model.NewOrderRequest;
import com.example.demo.mybatis.service.CoffeeService;
import com.example.demo.mybatis.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: YsCy丶
 * @Date: 2020/8/5 16:18
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class CoffeeOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CoffeeService coffeeService;

    @RequestMapping(value = "/",method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public CoffeeOrder create(@RequestBody NewOrderRequest newOrder){
        log.info("Receive new Order {}",newOrder);
        List<Coffee> coffees = coffeeService.findOneCoffeeByName(newOrder.getCoffeeName());
        log.info("Find {} Coffee",coffees.size());
        return orderService.createOrder(newOrder.getCustomer(),coffees.get(0));
    }
}
